package teste;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import com.hp.hpl.jena.ontology.Individual;
import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntDocumentManager;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.ontology.OntResource;
import com.hp.hpl.jena.rdf.model.ModelFactory;

import fuzzyOntologyFramework.FuzSemRules;

public class Teste {

	public static void main(String[] args) {
		testeSupermercado();

	}

	public static void testeSupermercado() {
		Hashtable ontologies = new Hashtable();
		
		// ontologies and paths
		String ontName = "http://www.owl-ontologies.com/supermercado.owl";
		String ontLocation = "file:D://Eclipse//Workspaces//ContextFuzzyOntologyFramework//ontologies//supermercadoContexto.owl";
		String ontNs = "http://www.owl-ontologies.com/supermercado.owl#";
		
		String fuzzyOntName = "http://www.owl-ontologies.com/fuzzyOntology.owl";
		String fuzzyOntLocation = "file:D://Eclipse//Workspaces//ContextFuzzyOntologyFramework//ontologies//fuzzyOntology.owl";
		String fuzzyOntNs = "http://www.owl-ontologies.com/fuzzyOntology.owl#";
		
		String ctxFuzzyOntName = "http://www.owl-ontologies.com/fuzzyContextOntology.owl";
		String ctxFuzzyOntLocation = "file:D://Eclipse//Workspaces//ContextFuzzyOntologyFramework//ontologies//fuzzyContextOntology.owl";
		String ctxFuzzyOntNs = "http://www.owl-ontologies.com/fuzzyContextOntology.owl#";
		
		ontologies.put(ontName, ontLocation);
		ontologies.put(fuzzyOntName, fuzzyOntLocation);
		ontologies.put(ctxFuzzyOntName, ctxFuzzyOntLocation);
		OntModel ontModel = loadOntologies(ontologies);
	
		FuzSemRules fuzSemRules = new FuzSemRules(ontModel);
		
		// parameters
		float minMembership = new Float(0.1).floatValue();
		float minSimilarity = new Float(0.1).floatValue();
		
		// individuos e classes de supermercado
		OntProperty simTo = ontModel.getOntProperty(fuzzyOntNs + "similarTo");
		
		OntClass produto = ontModel.getOntClass(ontNs + "Produto");
		OntClass alimenticeo = ontModel.getOntClass(ontNs + "Alimenticeo");
		OntClass fruta = ontModel.getOntClass(ontNs + "Fruta");
		OntClass legume = ontModel.getOntClass(ontNs + "Legume");
		OntClass hortifruti = ontModel.getOntClass(ontNs + "Hortifruti");
		OntClass comida = ontModel.getOntClass(ontNs + "Comida");
		Individual pera = ontModel.getIndividual(ontNs + "pera");
		Individual maca = ontModel.getIndividual(ontNs + "maca");
		Individual tomate = ontModel.getIndividual(ontNs + "tomate");
		
		// testing context methods
		Individual ind1 = tomate;
		OntClass ontClass = fruta;
		
		// getting all contexts
		OntClass ctxClass = ontModel.getOntClass(ctxFuzzyOntNs + "Context");
		for (Iterator i = ctxClass.listInstances(); i.hasNext();) {
			Individual ctx = ((OntResource) i.next()).as(Individual.class);
			
			System.out.println("----------------------------------");
			System.out.println("Contexto = " + ctx.getLocalName());
			System.out.println("----------------------------------");
			
			// getting similar individuals to ind1		
			Hashtable similarIndividuals = fuzSemRules.getSimilarToByContext(ind1, ctx, minSimilarity);
			if (similarIndividuals != null) {
				for (Enumeration e = similarIndividuals.keys(); e.hasMoreElements();) {
					Individual ind2  = (Individual) e.nextElement();
					float memb = ((Float) similarIndividuals.get(ind2)).floatValue();
					System.out.println("similarTo(" + ind1.getLocalName() + ", " + ind2.getLocalName() + ") = " + memb );
				}
				System.out.println();
			}
			
			
			
			// getting membership degrees between ind1 and ontClass 
			float membDegree = fuzSemRules.getMembDegreeByContext(ind1, ontClass, ctx, minMembership);
			System.out.println(ontClass.getLocalName() + "(" + ind1.getLocalName() + ") = " + membDegree );
			System.out.println();
			
			
			
			// getting classes of ind1 and membership degrees		
			Hashtable membClasses = fuzSemRules.getClassesByContext(ind1, ctx, minMembership);
			if (membClasses != null) {
				for (Enumeration e = membClasses.keys(); e.hasMoreElements();) {
					OntClass ontClass2  = (OntClass) e.nextElement();
					float memb = ((Float) membClasses.get(ontClass2)).floatValue();
					System.out.println(ontClass2.getLocalName() + "(" + ind1.getLocalName() + ") = " + memb );
				}
				System.out.println();
			}
			
		}
	
	}
	
	public static OntModel loadOntologies(Hashtable domainOntologies) {
		
		OntDocumentManager docMgr1 = new OntDocumentManager();
		
		// getting ontology names and location
		for (Enumeration e = domainOntologies.keys(); e.hasMoreElements();) {
			String ontName = (String) e.nextElement();
			String ontLocation = (String) domainOntologies.get(ontName);
			docMgr1.addAltEntry(ontName, ontLocation);
		}
		
		// reading ontologies
		OntModelSpec ms1 = new OntModelSpec( OntModelSpec.OWL_MEM_MICRO_RULE_INF);
		ms1.setDocumentManager( docMgr1 );
		OntModel ontModel = ModelFactory.createOntologyModel(ms1);
		
		for (Enumeration e = domainOntologies.keys(); e.hasMoreElements();) {
			String ontName = (String) e.nextElement();
			ontModel.read(ontName);
		}
		
		return ontModel;
		
	}
	
}
